replace
¶'I applied to UofU'.replace('UofU', 'BYU')
'I applied to BYU'
replace
let's you replace a substring with another string.
'A normal looking phrase'.replace(' ', '-')
'A-normal-looking-phrase'
By default, replace
replaces all occurrences of the substring with its substitute.
'I like cats and cats'.replace('cats', 'dogs', 1)
'I like dogs and cats'
You can provide a 3rd argument to replace
to control how many substitutions can be made.
text = 'I like to eat pizza'
new_text = text.replace('pizza', 'soup')
print(text)
print(new_text)
I like to eat pizza I like to eat soup
replace
(and any other string function that returns a string) returns a new string.
It doesn't modify the input string.
in
¶'BYU' in 'I am a student at BYU.'
True
'BYU' in 'This room is full of monkeys!'
False
Use the in
operator to determine whether a substring is present in a string.
def is_vowel(letter):
return letter in 'AEIOUaeiou'
found = ''
for letter in 'The Aeneid is ancient Greek literature.':
if is_vowel(letter):
found += letter
print(found)
eAeeiiaieeeieaue
def is_vowel(letter):
return letter.lower() in 'aeiou'
found = ''
for letter in 'The Aeneid is ancient Greek literature.':
if is_vowel(letter):
found += letter
print(found)
eAeeiiaieeeieaue
Capitalize every letter in an input string that is part of BYU
.
def isbyu(letter):
return letter.lower() in 'byu'
def byu(text):
"""Capitalize every letter of `text` that is part of 'BYU'"""
found = ''
for letter in text:
if isbyu(letter):
letter = letter.upper()
found += letter
return found
print(byu('write "byu" in all-caps. By byu!'))
write "BYU" in all-caps. BY BYU!
print(byu('Any student, boy or girl, young or old, can be a yodeler.'))
AnY stUdent, BoY or girl, YoUng or old, can Be a Yodeler.
def is_bracket(letter):
return letter in '{}[]<>'
def has_brackets(text):
for letter in text:
if is_bracket(letter):
return True
return False
has_brackets('Just some words')
False
has_brackets('A Python list prints like this: [1, 2, 3, 4]')
True
Write a function that indicates whether a string has odd-numbered digits in it.
def is_odd_number(letter):
return letter in '13579'
def has_odds(text):
"""Return True if the text has odd-numbered digits"""
for letter in text:
if is_odd_number(letter):
return True
return False
has_odds('I have 2 apples to share with 4 people.')
False
has_odds('I have 2 apples, and there are 34 people, but I will eat them all!')
True
Write a function that indicates whether a text has any of the following substrings in it:
def is_true_blue(text):
"""Returns True if `text` contains any of: 'BYU', 'blue', or 'cougar'"""
blue_pride = ['BYU', 'blue', 'cougar', 'Reese', 'Provo']
for word in blue_pride:
if word in text:
return True
return False
is_true_blue('My friend goes to UVU')
False
is_true_blue('I love BYU!')
True
is_true_blue('The sky is blue')
True
is_true_blue('Don\' touch the cougar')
True
in
and list
¶numbers = [1, 5, 9]
print(4 in numbers)
print(5 in numbers)
False True
words = ['foo', 'bar', 'baz']
print('pizza' in words)
print('baz' in words)
False True
def identify_suspects(people: list, possible_suspects: list):
"""Return a list of people who are possible suspects"""
suspects = []
for person in people:
if person in possible_suspects:
suspects.append(person)
return suspects
possible_suspects = ['John', 'Jane', 'Susan', 'Carlos', 'Kathy', 'Morgan']
students = ['George', 'Hannah', 'Kathy', 'Michael', 'John']
print(identify_suspects(students, possible_suspects))
['Kathy', 'John']
neighbors = ['Carl', 'Brooke', 'Jane', 'Jarom', 'Sally', 'John', 'Mike']
print(identify_suspects(neighbors, possible_suspects))
['Jane', 'John']
Are there any suspected neighbors that are also suspected students?
def find_key_suspects(students, neighbors, possible_suspects):
"""Identify persons who are both students and neighbors and are suspect"""
suspects = []
for person in possible_suspects:
if person in students and person in neighbors:
suspects.append(person)
return suspects
find_key_suspects(students, neighbors, possible_suspects)
['John']
Write a program that queries a user for a list of fruits.
If the user says a fruit that has already been given, say "You already said that."
The program should ignore casing (uppercase vs lowercase).
Once 10 fruits have been given, print "Way to go!" and end the program.
fruits.py
¶Fruit: pear Fruit: apple Fruit: Pear You already said that. Fruit: kiwi Fruit: BANANA Fruit: pear You already said that. Fruit: APPLE You already said that. Fruit: Orange Fruit: lemon Fruit: lime Fruit: cherry Fruit: orange You already said that. Fruit: guava Fruit: plum Way to go!
replace
in
in
in
and list